﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaBrowser.Library.Plugins;
using System.Reflection;
using System.IO;
using MediaBrowser.Library.Logging;

namespace MusicPluginProxy
{

    public static class Global
    {        
        public static string PLUGIN_NAME = "Music support.";
        public static string PLUGIN_DESCRIPTION = "Music library plugin for MediaBrowser by Nephelyn.";
        public static System.Version PLUGIN_VERSION = Assembly.GetExecutingAssembly().GetName().Version;
        public static System.Version PLUGIN_REQUIRED_MB_VERSION = new System.Version(2, 2, 2, 0);
        public static System.Version PLUGIN_TESTED_MB_VERSION = new System.Version(2, 2, 2, 0);
        public static bool PLUGIN_IS_CONFIGURABLE = true;
        public static string PLUGIN_RICHURL = @"http://www.craigthomas.co.za/MusicPlugin/MusicPlugin.html";
    }

    public class Plugin:BasePlugin
    {
        IPlugin _plugin;

        public override void Init(MediaBrowser.Library.Kernel kernel)
        {
            try
            {
                Logger.ReportInfo(string.Format("Tyring to load {0} v{1} loaded by {2}.", "MusicPluginProxy", LatestVersion.ToString(), AppDomain.CurrentDomain.FriendlyName));
                
                AssemblyUtils.ExtractResources(kernel.ConfigData.InitialFolder + @"\..\Plugins");
                AssemblyUtils.RegisterAssemblyResolvers(kernel.ConfigData.InitialFolder + @"\..\Plugins");

                if (File.Exists(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin.xml") && 
                    !File.Exists(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\MusicPlugin.xml"))
                    File.Move(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin.xml", kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\MusicPlugin.xml");

                if (File.Exists(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin.dll"))
                {
                    if (!Directory.Exists(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\Backup"))
                        Directory.CreateDirectory(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\Backup");

                    if (!File.Exists(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\Backup\MusicPlugin.dll"))
                        File.Move(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin.dll", kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\Backup\MusicPlugin.dll");
                    else
                        File.Delete(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\MusicPlugin.dll");
                }
                Assembly pluginAssembly =
                    Assembly.Load(System.IO.File.ReadAllBytes(kernel.ConfigData.InitialFolder + @"\..\Plugins\MusicPlugin\MusicPlugin.dll"));
                _plugin = (IPlugin)pluginAssembly.CreateInstance("MusicPlugin.Plugin");
                _plugin.Init(kernel);
            }
            catch (Exception e)
            {
                Logger.ReportException("Cannot load MusicPluginProxy", e);
            }
        }       

        public override string Name
        {
            get { return _plugin==null?Global.PLUGIN_NAME:_plugin.Name; }
        }

        public override string Description
        {
            get { return _plugin == null ? Global.PLUGIN_DESCRIPTION : _plugin.Description; }
        }

        public override System.Version LatestVersion
        {
            get
            {
                return _plugin == null ? Global.PLUGIN_VERSION : _plugin.Version;
            }
            set
            {
                //base.LatestVersion = value;
            }
        }

        public override string RichDescURL
        {
            get
            {
                return Global.PLUGIN_RICHURL;
            }
        }

        public override System.Version Version
        {
            get
            {
                return _plugin == null ? Global.PLUGIN_VERSION : _plugin.Version;
            }
        }

        public override Version RequiredMBVersion
        {
            get
            {
                return _plugin == null ? Global.PLUGIN_REQUIRED_MB_VERSION : _plugin.RequiredMBVersion;
            }
        }

        public override Version TestedMBVersion
        {
            get
            {
                return _plugin == null ? Global.PLUGIN_TESTED_MB_VERSION : _plugin.TestedMBVersion;
            }
        }

        public override bool IsConfigurable
        {
            get
            {
                return _plugin == null ? Global.PLUGIN_IS_CONFIGURABLE : _plugin.IsConfigurable;
            }
        }

        public override void Configure()
        {
            _plugin.Configure();
        }     
   

    }
}
